package com.bzkj.basicinfo.service.impl.ncc;

import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bzkj.basicinfo.domain.ncc.NccBasicReducedUnit;
import com.bzkj.basicinfo.domain.ncc.dto.NccBasicReducedUnitDto;
import com.bzkj.basicinfo.mapper.NccBasicReducedUnitMapper;
import com.bzkj.basicinfo.mapper.NccBasicSkuMapper;
import com.bzkj.basicinfo.service.ncc.NccBasicReducedUnitService;
import com.bzkj.basicinfo.utils.DataBatchProcessorUtil;
import com.bzkj.basicinfo.utils.HttpClientUtils;
import com.bzkj.basicinfo.utils.UniqueIdentifierGenerator;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Version;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author wangzhichao
 * @ClassName NccBasicReducedUnitServiceImpl
 * @Date 2024/12/09 15:32:39
 * @description Ncc物料转化系数：4
 */
@Slf4j
@Service("nccBasicReducedUnitServiceImpl")
public class NccBasicReducedUnitServiceImpl implements NccBasicReducedUnitService {
    @Autowired
    private NccBasicReducedUnitMapper nccBasicReducedUnitMapper;

    @Autowired
    private UniqueIdentifierGenerator uniq;

    @Autowired
    private HttpClientUtils hcu;

    @Autowired
    private NccBasicSkuMapper nccBasicSkuMapper;

    @Value("${pk_org.code}")
    private Integer pkOrgCode;

    /**
     * 根据条件分页查询数据
     *
     * @Author wangzhichao
     * @Date 2024/12/09 15:48:37
     * @Param dto
     */
    @Override
    public List<NccBasicReducedUnit> findNccBasicReducedUnitData(NccBasicReducedUnitDto dto) {
        return nccBasicReducedUnitMapper.findNccBasicReducedUnitData(dto);
    }

    /**
     * 根据pk_material修改信息
     *
     * @Author wangzhichao
     * @Date 2024/12/09 16:21:41
     * @Param entity
     */
    @Override
    public AjaxResult nccBasicReducedUnitUpdate(NccBasicReducedUnit entity) {
        try {
            int i = nccBasicReducedUnitMapper.nccBasicReducedUnitUpdate(entity);
            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            log.error("修改失败:{}", e);
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 新增换算系数信息
     *
     * @Author wangzhichao
     * @Date 2024/12/10 10:57:15
     * @Param entity
     */
    @Override
    public AjaxResult nccBasicReducedUnitSave(NccBasicReducedUnit entity) {
        synchronized (this) {
            String key = "WMS" + uniq.generateUniqueIdentifier();
            entity.setpK_MATERIAL(key);
        }
        entity.setTs(new Date());
        try {
            nccBasicReducedUnitMapper.nccBasicReducedUnitSave(entity);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            log.error("新增失败:{}", e);
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 定时任务同步当前数据
     *
     * @return
     */
    @Version
    public R jobMethodExecute(Boolean flag) {
        HashMap<String, String> searchDate = DataBatchProcessorUtil.getSearchDate(flag);

        // 查询当前数据
        HttpResponse response = hcu.methodExecute(searchDate.get("startTimeStr"), searchDate.get("endTimeStr"), 4, pkOrgCode);
        // 收集rep系统数据
        String responseBody = response.body();
        JSONObject entries = JSONUtil.parseObj(responseBody);
        JSONArray dataArray = entries.getJSONArray("data");
        List<NccBasicReducedUnit> nccBasicReducedUnits = dataArray.toList(NccBasicReducedUnit.class);
        if (!nccBasicReducedUnits.isEmpty())
            extracted(nccBasicReducedUnits);

        return R.ok("同步成功");
    }


    /**
     * 批量更新或者插入
     *
     * @Date 2024/12/11 14:17:12
     * @Param list
     */
    @Transactional(rollbackFor = Exception.class)
    public void extracted(List<NccBasicReducedUnit> list) {
        for (NccBasicReducedUnit nccBasicReducedUnit : list) {
            nccBasicReducedUnitMapper.insertOrUpdate(nccBasicReducedUnit);
        }
    }

}
